<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-03-20 09:48:35 +0100 (Thu, 20 Mar 2008) $
 * $Rev: 1040 $
 * $Author: styck $
 * $Id: install.4.inc.php 1040 2008-03-20 08:48:35Z styck $
 */

// This is an include-only file:
if (!defined('ORIGINAL_PAGE')) die("This file cannot be accessed directly.");

// Check database account settings
$OUT .= '<tr><td colspan="2" class="install1">Checking database</td></tr>' . "\n";

if(!function_exists('echo_debug')) { function echo_debug(){}; }
if(!function_exists('echo_debug_function')) { function echo_debug_function(){}; }
if(!function_exists('write_log')) { function write_log(){}; }

$dbtype = $_REQUEST['dbtype'];
$dbhost = $_REQUEST['dbhost'];
$dbname = $_REQUEST['dbname'];
$dbuser = $_REQUEST['dbuser'];
$dbpass = $_REQUEST['dbpass'];
$dbruser = $_REQUEST['dbruser'];
$dbrpass = $_REQUEST['dbrpass'];
$dbclear = isset($_REQUEST['dbclear']) ? TRUE : FALSE;

// Store in session:
$_SESSION['dbtype'] = $dbtype;
$_SESSION['dbhost'] = $dbhost;
$_SESSION['dbname'] = $dbname;
$_SESSION['dbuser'] = $dbuser;
$_SESSION['dbpass'] = $dbpass;
$_SESSION['dbruser'] = $dbruser;
$_SESSION['dbrpass'] = $dbrpass;
$_SESSION['dbclear'] = $dbclear; 

require_once('../functions/db.class.php');
require_once('../functions/functions.php');
require_once('../functions/defaults.php');
require_once('../functions/buttons.php');
// Sanity check:
$skip = FALSE;
if ($dbruser != '' && $dbclear && $dbuser == 'root') {
	$OUT .= '<tr colspan="2"><td>This installer will not allow you to delete the \'root\' user! Change \'Database username\' to something else or deselect \'Delete existing db &amp; user\'!</td></tr>';
	$OUT .= '<tr colspan="2"><td><a onclick="LoadPage(3);">'.$refreshpic.'</a></td></tr>';
    $skip = TRUE;
    $dbclear = FALSE;
}

$db_created = TRUE;

if ($dbruser != '' || $dbtype == 'sqlite') // = we need to log in as root and create the user and database:
{
    $OUT .= '<tr><td class="install2">Database root login</td>';
    $clean_user = $dbruser;
    $clean_pw = $dbrpass;
    try {
        $db = new DatabaseConnection($dbtype, $dbhost, $dbruser, $dbrpass,'', TRUE);
        $rv_rdb = TRUE;
    } catch (exception $e) {
        $rv_rdb = FALSE;
        $error = $e->getMessage();
        $db_created = FALSE;
    }
    $OUT .= GenRetVal($rv_rdb);
    if ($rv_rdb == FALSE)
        $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';
    // If database or user already exist, clear them ?
    if ($dbclear) {
        $OUT .= '<tr><td class="install2">Deleting old database</td>';
        try{
            if ($rv_rdb === FALSE) 
                throw new exception("Root login required");
            $db->drop_database($dbname);
            $rv_deldb = TRUE;
        } catch (exception $e) {
            $rv_deldb = FALSE;
            $error = $e->getMessage();
            $db_created = FALSE;
        }
        $OUT .= GenRetVal($rv_deldb);
        if ($rv_deldb == FALSE)
            $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';

        $OUT .= '<tr><td class="install2">Deleting old user</td>';

        try{
            if ($rv_rdb === FALSE) throw new exception("Root login required");
            $db->drop_user($dbuser, $dbhost);
            $rv_delu = TRUE;
        } catch (exception $e) {
            $rv_delu = FALSE;
            $error = $e->getMessage();
            $db_created = FALSE;
        }
        $OUT .= GenRetVal($rv_delu);
        if ($rv_delu == FALSE)
            $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';
    }

    $OUT .= '<tr><td class="install2">Database creation</td>';
    try{
        if ($rv_rdb === FALSE) throw new exception("Root login required");
        $db->create_database($dbname);
        $rv_cdb = TRUE;
    } catch (exception $e) {
        $rv_cdb = FALSE;
        $db_created = FALSE;
        $error = $e->getMessage();
    }
    $OUT .= GenRetVal($rv_cdb);
    if ($rv_cdb == FALSE)
        $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';

    $OUT .= '<tr><td class="install2">Database account creation</td>';
    try{
        if ($rv_rdb === FALSE) throw new exception("Root login required");
        if ($rv_cdb === FALSE) throw new exception("Database not created");
        $db->create_dbuser($dbhost, $dbuser, $dbpass);
        $rv_cua = TRUE;
    } catch (exception $e) {
        $rv_cua = FALSE;
        $db_created = FALSE;
        $error = $e->getMessage();
    }
    $OUT .= GenRetVal($rv_cua);

    if ($rv_cua == FALSE)
        $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';

    $OUT .= '<tr><td class="install2">Database account privileges</td>';
    try{
        if ($rv_rdb === FALSE) 
            throw new exception("Root login required");
        if ($rv_cdb === FALSE) throw new exception("Database not created");
        if ($rv_cua === FALSE) throw new exception("Database user not created");
        $db->grant_rights($dbhost, $dbname, $dbuser);
        $rv_cup = TRUE;
    } catch (exception $e) {
        $rv_cup = FALSE;
        $error = $e->getMessage();
        $db_created = FALSE;
    }
    $OUT .= GenRetVal($rv_cup);
    if ($rv_cup == FALSE)
        $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';

}
if ($db_created) {
    $OUT .= '<tr><td class="install2">Database user connection</td>';

    try {
        $db = new DatabaseConnection($dbtype, $dbhost, $dbuser, $dbpass,$dbname, TRUE);
        $rv_db = TRUE;
    } catch (exception $e) {
        $rv_db = FALSE;
        $error = $e->getMessage();
    }
    $OUT .= GenRetVal($rv_db);
    if ($rv_db === FALSE)
        $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';

    if($dbruser != '') {
        $OUT .= '<tr><td class="install2">Creating tables</td>';
        try{
            if ($rv_db === FALSE) throw new exception('User login required');
            $SQLcommands = create_database_structure($dbtype);
            foreach ($SQLcommands as $query) {
                $db->execute_query($query);
            }
            $rv_ct = TRUE;
        } catch (exception $e) {
            $rv_ct = FALSE;
            $error = $e->getMessage();
        }
        $OUT .= GenRetVal($rv_ct);
        if ($rv_ct == FALSE)
            $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';

        // Load default DB
        $OUT .= '<tr><td class="install2">Creating URD root account</td>';
        try {
            if ($rv_db === FALSE) throw new exception('User login required');
            // Make root user:
            create_root($db);
            $rv_cr = TRUE;
        } catch (exception $e) {
            $rv_cr = FALSE;
            $error = $e->getMessage();
        }
        $OUT .= GenRetVal($rv_cr);
        if ($rv_cr == FALSE)
            $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';
    } else {
        $rv_ct = TRUE;
        $rv_cr = TRUE;
    }

    // Saving the DB data
    if ($rv_db && $rv_ct && $rv_cr) {
        $OUT .= '<tr><td class="install2">Saving DB information</td>';
        try {
            WriteDBConfig($dbtype,$dbuser,$dbpass,$dbname,$dbhost);
            $rv_sdi = TRUE;
        } catch (exception $e) {
            $rv_sdi = FALSE;
            $error = $e->getMessage();
        }
        $OUT .= GenRetVal($rv_sdi);

        if ($rv_sdi == FALSE)
            $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';
    }
    // Storing default usenet servers:
    if ($rv_db && $rv_ct && $rv_cr) {
        $OUT .= '<tr><td class="install2">Storing default usenet servers</td>';
        try {
            create_usenet_servers($db);
            $rv_cus = TRUE;
        } catch (exception $e) {
            $rv_cus = FALSE;
            $error = $e->getMessage();
        }
        $OUT .= GenRetVal($rv_cus);

        if ($rv_cus == FALSE)
            $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';
    }

    // Storing default buttons:
    if ($rv_db && $rv_ct && $rv_cr)
    {
        $OUT .= '<tr><td class="install2">Storing default search buttons</td>';
        try{
            insert_default_buttons($db);
            $rv_idb = TRUE;
        } catch (exception $e) {
            $rv_idb = FALSE;
            $error = $e->getMessage();
        }
        $OUT .= GenRetVal($rv_idb);

        if ($rv_idb == FALSE)
            $OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';
    }
}

if ($rv_db && $rv_ct && $rv_cr && $rv_sdi && $rv_cus) {
    $OUT .= '<tr colspan="2"><td><a onclick="LoadPage(5);">'.$continuepic.'</a>';
    $OUT .= '<a onclick="LoadPage(3);">'.$refreshpic.'</a></td></tr>';
} else {
    $OUT .= 'Please make sure you have the correct database details.<br/>';
    $OUT .= '<tr colspan="2"><td><a onclick="LoadPage(3);">'.$refreshpic.'</a></td></tr>';
}

?>
